import com.zhangrui.dao.BlogMapper;
import com.zhangrui.pojo.Blog;
import com.zhangrui.utils.IDutils;
import com.zhangrui.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * ClassName:Text
 * Package:PACKAGE_NAME
 * Description:
 *
 * @Date:2021/8/2521:19
 * @Author:zhangrui@qq.com
 */
public class Text {
    static Logger logger = Logger.getLogger(Test.class);

    @Test
    public void text1() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        Blog blog = new Blog();
        blog.setId(IDutils.getId());
        blog.setTitle("卧槽无情啊");
        blog.setAutor("张瑞");
        blog.setCreateTime(new Date());
        blog.setViews(9999);
        blogMapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("卧槽无情啊1");
        blog.setAutor("张瑞1");
        blogMapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("卧槽无情啊2");
        blog.setAutor("张瑞2");
        blogMapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("卧槽无情啊3");
        blog.setAutor("张瑞3");
        blogMapper.addBlog(blog);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test2() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("title", "卧槽无情啊");
//        map.put("autor", "张瑞");
        List<Blog> blogs = blogMapper.queryBlogIF(map);
        logger.info(blogs);
        sqlSession.close();
    }

    @Test
    public void test3() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
//        map.put("title", "卧槽无情啊");
        map.put("autor", "张瑞1");
//        map.put("views", "9999");
        List<Blog> blogs = blogMapper.queryBlogChoose(map);
        logger.info(blogs);
        sqlSession.close();
    }

    @Test
    public void test4() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("title", "卧槽无情啊");
        map.put("autor", "张瑞1");
        map.put("id", "28de634d12de4c878624872f8e553721");
        int i = blogMapper.updateSet(map);
        logger.info("更新了" + i + "条数据！！！");
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test5() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        ArrayList arrayList = new ArrayList();
        arrayList.add(1);
        arrayList.add(8);
        arrayList.add(9);
        System.out.println("数组:" + arrayList);
        hashMap.put("ids", arrayList);
        System.out.println("map集合：" + hashMap);
        List<Blog> blogList = blogMapper.queryBlogForeach(hashMap);
        for (Blog blog : blogList) {
            logger.info(blog);
        }
        sqlSession.close();
    }
}
